﻿namespace ImageProcessing
{
    /// <summary>
    /// Bitmap缩放工具类
    /// </summary>
    public class UtilBitmapScale
    {
        /// <summary>
        /// 调整image尺寸大小到newWidth\newHeight.
        /// </summary>
        /// <param name="image"></param>
        /// <param name="newWidth"></param>
        /// <param name="newHeight"></param>
        /// <returns></returns>
        public Bitmap ResizeImageLossless(Bitmap image, int newWidth, int newHeight)
        {
            Bitmap resizedImage = new Bitmap(newWidth, newHeight);
            using (Graphics graphics = Graphics.FromImage(resizedImage))
            {
                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graphics.DrawImage(image, 0, 0, newWidth, newHeight);
            }

            return resizedImage;
        }

        /// <summary>
        /// 根据radio调整image尺寸大小
        /// </summary>
        /// <param name="image"></param>
        /// <param name="radio"></param>
        /// <returns></returns>
        public static Bitmap ResizeImageLossless(Bitmap image, double radio)
        {
            int newWidth = (int)(image.Width * radio);
            int newHeight = (int)(image.Height * radio);
            Bitmap resizedImage = new Bitmap(newWidth, newHeight);

            using (Graphics graphics = Graphics.FromImage(resizedImage))
            {
                graphics.DrawImage(image, new Rectangle(0, 0, newWidth, newHeight));
            }
            return resizedImage;
        }

    }
}
